package zd.cornermemory.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pgyer.pgyersdk.PgyerSDKManager;
import com.pgyer.pgyersdk.callback.CheckoutCallBack;
import com.pgyer.pgyersdk.model.CheckSoftModel;

import zd.cornermemory.R;

public class AboutActivity extends BaseActivity implements View.OnClickListener{
    private TextView version_text;
    private RelativeLayout layout_00;
    private RelativeLayout layout_0;
    private RelativeLayout layout_1;
    private RelativeLayout layout_2;
    private RelativeLayout layout_3;
    private RelativeLayout layout_4;
    private Toast t ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        t = Toast.makeText(AboutActivity.this,getString(R.string.new_),Toast.LENGTH_SHORT);

        version_text = findViewById(R.id.version_text);
        layout_00 = findViewById(R.id.layout_00);
        layout_0 = findViewById(R.id.layout_0);
        layout_1 = findViewById(R.id.layout_1);
        layout_2 = findViewById(R.id.layout_2);
        layout_3 = findViewById(R.id.layout_3);
        layout_4 = findViewById(R.id.layout_4);
        String name = getAppVersionName(mContext);
        version_text.setText("Version "+name);
        setBackImage();
        layout_00.setOnClickListener(this);
        layout_0.setOnClickListener(this);
        layout_1.setOnClickListener(this);
        layout_2.setOnClickListener(this);
        layout_3.setOnClickListener(this);
        layout_4.setOnClickListener(this);

    }

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName=null;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.layout_00:
                intent = new Intent(mContext, ShareActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_0:
                PgyerSDKManager.checkVersionUpdate(AboutActivity.this, new CheckoutCallBack() {
                    @Override
                    public void onNewVersionExist(final CheckSoftModel model) {
                        //检查版本成功（有新版本）

                        /**
                         *   CheckSoftModel 参数介绍
                         *
                         *    private int buildBuildVersion;//蒲公英生成的用于区分历史版本的build号
                         *     private String forceUpdateVersion;//强制更新版本号（未设置强置更新默认为空）
                         *     private String forceUpdateVersionNo;//强制更新的版本编号
                         *     private boolean needForceUpdate;//	是否强制更新
                         *     private boolean buildHaveNewVersion;//是否有新版本
                         *     private String downloadURL;//应用安装地址
                         *     private String buildVersionNo;//上传包的版本编号，默认为1 (即编译的版本号，一般来说，编译一次会
                         *    变动一次这个版本号, 在 Android 上叫 Version Code。对于 iOS 来说，是字符串类型；对于 Android 来
                         *    说是一个整数。例如：1001，28等。)
                         *     private String buildVersion;//版本号, 默认为1.0 (是应用向用户宣传时候用到的标识，例如：1.1、8.2.1等。)
                         *     private String buildShortcutUrl;//	应用短链接
                         *     private String buildUpdateDescription;//	应用更新说明
                         */
                        new AlertDialog.Builder(AboutActivity.this)
                                .setTitle(getString(R.string.found_new))
                                .setMessage(model.getBuildUpdateDescription())
                                .setCancelable(false)
                                .setNegativeButton(getString(R.string.cancel),null)
                                .setPositiveButton(getString(R.string.download), new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getBuildShortcutUrl()));
                                        startActivity(intent);
                                        dialog.dismiss();
                                    }
                                }).show();

                    }

                    @Override
                    public void onNonentityVersionExist(String string) {
                        //无新版本
                        t.show();
                    }

                    @Override
                    public void onFail(String error) {
                        //请求异常
                    }
                });
                break;
            case R.id.layout_1:
                intent = new Intent(mContext, AuthorActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_2:
                intent = new Intent(mContext, LogActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_3:
                intent = new Intent(mContext, AgreementActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_4:
                intent = new Intent(mContext, PrivacyActivity.class);
                startActivity(intent);
                break;
        }
    }
}
